#include <iostream>
using namespace std;
#include <vector>


//version 1 
class Solution {
public:
    int findPoisonedDuration(vector<int>& timeSeries, int duration) {
        int ret = 0;
        int n = timeSeries.size();
        for(int i = 0; i < n; ++i){
            //非递减的情况，如果后面有一样的，直接给跳过去(可能是那一秒按了多次)
            if(i != n - 1 && timeSeries[i] == timeSeries[i + 1]) continue;

            int time_end = timeSeries[i] + duration - 1;
            if(i != n - 1 && time_end >= timeSeries[i + 1]) time_end = timeSeries[i + 1] - 1;
            ret += (time_end - timeSeries[i] + 1);
        }
        return ret;
    }
};